<?php

namespace common\models;

use Yii;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveRecord;

/**
 * This is the model class for table "flash".
 *
 * @property integer $id
 * @property string $flash_name
 * @property string $flash_url
 * @property integer $file_id
 * @property integer $created_at
 * @property integer $updated_at
 *
 * @property File $file
 */
class Flash extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'flash';
    }
    
    public function behaviors(){
        return [
            [
                'class' => TimestampBehavior::className(),
                'attributes' => [
                    ActiveRecord::EVENT_BEFORE_UPDATE => ['updated_at'],
                    ActiveRecord::EVENT_BEFORE_INSERT => ['created_at', 'updated_at'],
                ]
            ]
        ];
    }
    
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['flash_name', 'flash_url', 'file_id'], 'required'],
            [['file_id', 'created_at', 'updated_at'], 'integer'],
            [['flash_name'], 'string', 'max' => 32],
            [['flash_url'], 'string', 'max' => 255],
            [['flash_url'], 'url', 'defaultScheme' => 'http'],
            [['file_id'], 'exist', 'skipOnError' => true, 'targetClass' => File::className(), 'targetAttribute' => ['file_id' => 'file_id']],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => '排序',
            'flash_name' => '轮播名',
            'flash_url' => '跳转链接',
            'file_id' => '',
            'file' => '轮播图片'
        ];
    }
    
    /**
     * @return \yii\db\ActiveQuery
     */
    public function getFile()
    {
        return $this->hasOne(File::className(), ['file_id' => 'file_id']);
    }
    
    /**
     * @return array|ActiveRecord[]
     */
    public static function getList()
    {
        return static::find()->orderBy(['id' => SORT_DESC])->all();
    }
}
